$(function () {
    var collectionManager_datagrid = $("#collectionManager_datagrid");
    var collectionManager_dialog = $("#collectionManager_dialog");
    var collectionManager_form = $("#collectionManager_form");
    collectionManager_datagrid.datagrid({
        url: '/collectionManager/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'formalStudent.name', title: '学员姓名', width: 100,formatter:function (value,row) {
                return row.formalStudent?row.formalStudent.name:"";
            }},
            {field: 'formalStudent.intentionClass.className', title: '班级名', width: 100,formatter:function (value,row) {
                return row.formalStudent?row.formalStudent.intentionClass.className:"";
            }},
            {field: 'collectionTime', title: '收款时间', width: 100},
            {field: 'collectionAmount', title: '收款金额', width: 100},
            {field: 'formalStudent.debt', title: '学费欠款', width: 100,formatter:function (value,row) {
                return row.formalStudent?row.formalStudent.debt:"";
            }},
            {field: 'collectionType', title: '收款类型', width: 100,formatter:function(value){
                return value?value.name:"";
            }},
            {field: 'receiver', title: '收款人', width: 100,formatter:function(value){
                 return value?value.realname:"";
            }},
            {field: 'no', title: '单据号', width: 100},
            {field: 'formalStudent.salemen.realname', title: '营销人员', width: 100,formatter:function (value,row) {
                return row.formalStudent?row.formalStudent.salemen.realname:"";
            }},
            {field: 'remark', title: '备注', width: 100},
            {field: 'status', title: '审核状态', width: 100,formatter:function(value){
                return value?"已审核":"未审核";
            }},
            {field: 'auditor', title: '审核人员', width: 100,formatter:function(value){
                 return value?value.realname:"";
             }}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#collectionManager_btns",
        onClickRow: function (index, row) {
            if (row.status) {
                $("#audit_btn").linkbutton({
                    text: '取消审核'
                })
            } else {
                $("#audit_btn").linkbutton({
                    text: '审核'
                });
            }
        }
    });
    collectionManager_dialog.dialog({
        width: 400,
        height: 420,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            collectionManager_form.form('clear');
            $("#collectionManager_dialog").datagrid("reload");
            $("#student_combobox").combobox("reload");
        }

    });

    $("#student_combobox").combobox({
        onSelect:function (rec) {
            console.log(rec);
            $("#class_combobox").combobox('setValue',rec.intentionClass.id);
            $("#debt_textbox").textbox('setValue',rec.debt);
            $("#salemen_combobox").combobox('setValue',rec.salemen.id);
        }
    });

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    });
    var methodObject = {
        add: function () {
            $("#password_tr").show();
            collectionManager_dialog.dialog('setTitle', '新增');
            collectionManager_dialog.dialog('open');
        },

        edit: function () {
            var row = collectionManager_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            if(row.status){
                $.messager.alert('温馨提示', "已审核无法编辑", 'warning');
                return;
            }
            console.log(row.formalStudent.salemen.id);

            //回显角色数据
            if(row.formalStudent != null){
                row['formalStudent.id'] = row.formalStudent.id;
                row['formalStudent.intentionClass.id'] = row.formalStudent.intentionClass.id;
                row['formalStudent.salemen.debt'] = row.formalStudent.debt;
                row['receiver.id'] = row.receiver.id;
                row['collectionType.id']=row.collectionType.id;

                row['formalStudent.salemen.id'] = row.formalStudent.salemen.id;
            }


            collectionManager_form.form('load', row );
            collectionManager_dialog.dialog('setTitle', '编辑');
            collectionManager_dialog.dialog('open');
        },
        audit: function () {
            var row = collectionManager_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.post("/collectionManager/audit.do", {id: row.id,amount:row.collectionAmount,formalStudentId:row.formalStudent.id},
                        function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info');
                            collectionManager_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            collectionManager_datagrid.datagrid('reload');
        },

        save: function () {
            collectionManager_form.form('submit', {
                url: '/collectionManager/saveOrUpdate.do',
                success: function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    console.log(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info');
                        methodObject.cancel();
                        collectionManager_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            collectionManager_dialog.dialog('close');
        },


        query: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            collectionManager_datagrid.datagrid('load', {
                keyword: keyword,
                deptId: deptId
            })
        }


    }
})


